﻿using System;
using System.Collections.Generic;
using Distribution.Service;
using Entities;

namespace DistributionApplication
{
    public partial class frmMeter : frmBase
    {
        #region - Properties -

        public EPole Pole {get;set;}
        public EMeter MeterToBeLoaded { get; set; }
        
        #endregion

        #region - Constructor -

        public frmMeter()
        {
            InitializeComponent();
        }

        #endregion

        #region - Event handlers -

        private void frmMeter_Load(object sender, EventArgs e)
        {
            Title = "PPI | Add Meter";

            LButtonText = "Next";
            RButtonText = "Cancel";

            camControl.TempPhotoFileName = "MeterPhoto";
            pGPSViewer.Gps = m_GPS;
            pGPSViewer.RefreshPosition();

            if (Settings.DistributionApplicationMode == DistributionApplicationMode.GPS) {
                txtComments.Visible = false;
                label4.Visible = false;
            }

            LoadNoteList();

            if (Pole != null && Pole.ValidPosition)
                pGPSViewer.SetPosition(Pole.Latitude, Pole.Longitude);

            if (MeterToBeLoaded != null)
                LoadMeter(MeterToBeLoaded);
        }

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            switch (tbcMain.SelectedIndex)
            {
                case 0:
                    tbcMain.SelectedIndex = 1;
                    break;
                case 1:
                    SaveMeter();
                    camControl.DisableCamera();
                    Close();

                    break;
                default:
                    break;
            }
        }

        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            camControl.DisableCamera();
            Close();
        }

        private void tbcMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tbcMain.SelectedIndex)
            {
                case 0:
                    camControl.DisableCamera();
                    LButtonText = "Next";
                    break;
                case 1:
                    if ((MeterToBeLoaded == null || MeterToBeLoaded.Image == null) && camControl.GetTakenImages().Count == 0)
                        camControl.ActiveCamera();
                    
                    LButtonText = "Save";

                    break;
                default:
                    break;
            }
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lstNotes.SelectedIndex >= 0)
            {
                lstAddedNotes.Items.Add(lstNotes.SelectedItem);
                lstNotes.Items.Remove(lstNotes.SelectedItem);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstAddedNotes.SelectedIndex >= 0)
            {
                lstNotes.Items.Add(lstAddedNotes.SelectedItem);
                lstAddedNotes.Items.Remove(lstAddedNotes.SelectedItem);
            }
        }

        protected override void lblClose_Click(object sender, EventArgs e)
        {
            camControl.DisableCamera();
            Close();
        }

        private void txtMeterNumber_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }
        
        private void txtMeterNumber_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        private void txtComments_GotFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = true;
        }

        private void txtComments_LostFocus(object sender, EventArgs e)
        {
            iplKeyboard.Enabled = false;
        }

        #endregion

        #region - Private methods -

        private void LoadNoteList()
        {
            List<ELookUp> meterNotes = LLookUpService.GetAll(LookUpType.MeterNote);

            MobileUIHelper.FillListbox(lstNotes, meterNotes.ToArray(), null);
        }

        private void SaveMeter()
        {
            EMeter meter = new EMeter();

            meter.Pole = Pole;
            meter.Comments = txtComments.Text.Trim();
            meter.Latitude = pGPSViewer.Latitude;
            meter.Longitude = pGPSViewer.Longitude;

            List<EImage> photos = camControl.GetTakenImages();
            if (photos.Count > 0) 
            {
                meter.Image = photos[0];
                meter.Image.ImageType = ImageType.Meter;
            }

            meter.Number = txtMeterNumber.Text.Trim();
            meter.Notes = new List<ELookUp>();

            foreach (ELookUp note in lstAddedNotes.Items)
                meter.Notes.Add(note);

            if (MeterToBeLoaded == null)
                LMeter.Save(meter);
            else 
            {
                meter.Id = MeterToBeLoaded.Id;
                LMeter.Update(meter);
            }
        }

        private void LoadMeter (EMeter meter)
        {
            txtComments.Text = meter.Comments;

            if (meter.ValidPosition)
                pGPSViewer.SetPosition(meter.Latitude, meter.Longitude);

            if (meter.Image != null) 
            {
                camControl.SetTakenImages(meter.Image);
                camControl.SelectPicture(0);
            }

            txtMeterNumber.Text = meter.Number;
            
            foreach (ELookUp note in meter.Notes) 
            {
                lstAddedNotes.Items.Add(note);

                if (lstNotes.Items.Contains(note))
                    lstNotes.Items.Remove(note);
            }
        }

        #endregion
    }
}